<?php
/**
 * Created by PhpStorm.
 * User: xiaxiang
 * Date: 2019/3/14
 * Time: 10:20
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class NewsController
{
    public function detail($id = 0)
    {
        //接收传值
        $arr = [
            ['id' => 1, 'title' => 't1', 'content' => '新闻', 'type' => 1],
            ['id' => 2, 'title' => 't2', 'content' => '新闻', 'type' => 2],
            ['id' => 3, 'title' => 't3', 'content' => '新闻', 'type' => 3],
            ['id' => 4, 'title' => 't4', 'content' => '新闻', 'type' => 1],
            ['id' => 5, 'title' => 't5', 'content' => '新闻', 'type' => 2],
        ];
        $newArr = [];
        foreach ($arr as $v) {
            if ($v['id'] == $id) {
                //$newArr是一维的，找到以后就跳出
                $newArr = $v;
                break;
            }
        }
        return view('news/detail', ['title' => $newArr['title'],
            'content' => $newArr['content']
        ]);
    }

    //主页
    public function index($tid = 0)
    {
        //呈现文章列表页
        $arr = [
            ['id' => 1, 'title' => 't1', 'type' => 1],
            ['id' => 2, 'title' => 't2', 'type' => 2],
            ['id' => 3, 'title' => 't3', 'type' => 3],
            ['id' => 4, 'title' => 't4', 'type' => 1],
            ['id' => 5, 'title' => 't5', 'type' => 2],
        ];
        //呈现文章列表
        if ($tid == 0) {
            return view('news/index', ['arr' => $arr]);
        } else {
            $newArr = [];
            foreach ($arr as $v) {
                if ($v['type'] == $tid) {
                    $newArr[] = $v;
                }
            }
            return view('news/index', ['arr' => $newArr]);
        }
    }

    //提交评论
    public function doComment(Request $request)
    {
        //Request $request 对传值类型的约束 post请求过的时候需要加上
        //get是接收一个参数，all是接收所有参数
        $arr =$request->all();
        $content = $request->get('comment');
        dd($content);
    }

}